package com.shujia.mr.scoreCount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class ScoreCountReducer extends Reducer<LongWritable, IntWritable,LongWritable,IntWritable> {
    /**
     * Reduce端处理逻辑：
     *      1.将相同学生ID的数据拉去到同一个reduce函数中执行
     *      2.遍历同一个学生的所有成绩，进行累加
     *      3.将学生ID 和学生成绩写出到reduce端
     *
     * 编写代码过程：
     *      输入：Map端的输出
     *      输出：
     *          key：ID
     *          Value: 总分
     */

    @Override
    protected void reduce(LongWritable key, Iterable<IntWritable> values, Reducer<LongWritable, IntWritable, LongWritable, IntWritable>.Context context) throws IOException, InterruptedException {
//        super.reduce(key, values, context);

        // values 包含了同一个学生的所有科目成绩
        int sumScore =0;

        for (IntWritable score : values) {
            sumScore += score.get();
        }

        // 数据写出
        context.write(key,new IntWritable(sumScore));

    }
}
